package com.app.mingluxing.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.ResultBaseBean;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

/**
 * 修改电话
 * Created by Administrator on 2015/8/12.
 */
public class ModifyPhoneActivity extends BaseActivity {

    private static final int MODIFY_MY_PHONE_CODE = 11002;
    private EditText et_phone;
    private String id;
    private String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_modify_phone);
//        mApplication.addActivity(this);
        id = getIntent().getStringExtra("id");
        phoneNum = getIntent().getStringExtra("phoneNum");

        init();
    }

    @Override
     public void widgetClick(View v) {
        switch (v.getId()){

            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_right:

                if(CommonUtils.isNull(et_phone.getText().toString().trim())){
                    showToast("电话不能为空");
                    return;
                }
//                String modifyphone = et_phone.getText().toString().trim();
//
//                    SharedPreferences.Editor editor= sp.edit();
//                    editor.putString(Constant.MOBILE,modifyphone);
//                    editor.commit();
//                    Intent data = new Intent();
//                    data.putExtra("modifyphone", modifyphone);
//                    setResult(MODIFY_MY_Phone_CODE, data);
//                    finish();
                //更新手机号码
                updatePhoneNum();

                break;
        }
    }

    /**
     * 更新手机号码
     */
    private void updatePhoneNum() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("id", id);
        j.put("phoneNum", et_phone.getText().toString().trim());

        HttpUtil.setPostRequest("修改中...", this, Constant.WEB_ROOT + Constant.SETWATCHPHONE,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                    if (Constant.STATUS_SUCCESS == resultBaseBean.code) {

                        Intent data = new Intent();
                        data.putExtra("phone", et_phone.getText().toString().trim());
                        setResult(MODIFY_MY_PHONE_CODE, data);
                        finish();

                    }else
                        showToast(Constant.showErrorMsg(resultBaseBean.code, ModifyPhoneActivity.this));

                }catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, null);

    }

    private void init() {
        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_title.setText(getResources().getString(R.string.modify_phone));
        tv_right.setText(getResources().getString(R.string.done));
        et_phone= (EditText) findViewById(R.id.et_phone);

        et_phone.setHint(phoneNum);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

}
